﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PServiceBus.Core.Interface;
using PServiceBus.Core.Runtime.Transports;
using PServiceBus.Tcp;
using System.IO;
using System.Runtime.Serialization;
using System.ComponentModel.Composition;

namespace PServiceBus.Transports {
    [Serializable]
    [DataContract]
    [Export(typeof(ITransport))]
    public class TcpTransport : ITransport {
        [DataMember]
        public string IPAddress { get; set; }
        [DataMember]
        public int Port { get; set; }
        [DataMember]
        public bool UseSSL { get; set; }

        #region ITransport Members
        [DataMember]
        public TransportFormat Format { get; set; }

        public TransportResponse Send(string message, Dictionary<string, object> messageStructure) {
            var tranResponse = TransportResponse.New<TcpTransport>();
            try {
                Provider<string>.GetProvider(String.Format("{0}:{1}:{2}", IPAddress, Port, UseSSL))
                    .Add(message);
                tranResponse.Success = true;
            }
            catch (Exception ex) {
                tranResponse.Message = ex.ToString();
            }
            return tranResponse;
        }

        #endregion
    }
}
